package com.xj.util;


import com.xj.entity.XmlEntityVo;
import lombok.extern.slf4j.Slf4j;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.util.*;

@Slf4j
public class Bean2XmlUtils {

	/**
	 * 台账行号的最大长度
	 */
	private final static int INVENTORY_LINENUM_MAX_LENGTH = 50;
	private final static int SUB_PROD_CODE_MAX_LENGTH = 10;

	public static void createXML(String xmlFilepath,  XmlEntityVo xmlEntityVo) {
		String str = XStreamUtils.beanToXml(xmlEntityVo);

		 String  datatime = DateUtils.formatDate(new Date(), DateUtils.yyyyMMddHHmmss);
		String fullname = xmlFilepath +  File.separator+datatime+".xml" ;
		try {
			OutputStreamWriter outputStreamWriter = new OutputStreamWriter(
				new FileOutputStream(fullname), "utf-8");
			outputStreamWriter.write(str);
			outputStreamWriter.close();
		} catch (Exception e) {
			log.info("creat XML  err,cause:{}" + e.getMessage());
		}

	}


	public  static    void  saxToXml(String filepath,List<Map<String,Object>>  list ) {
		try {
			//用来生成XML文件
			// 要生成文件需构造PrintWriter的writer
			// 实现此接口的对象包含构建转换结果树所需的信息
			String  xmlFilepath=filepath.replace("todo","export");
			File destFile = new File(xmlFilepath);
			if(!destFile.isDirectory()){
				destFile.mkdirs();
			}

			String  datatime = DateUtils.formatDate(new Date(), DateUtils.yyyyMMddHHmmss);
			String fullname = xmlFilepath +  File.separator+datatime+".xml" ;
			Result resultXml = new StreamResult(new FileOutputStream(fullname));
			//用来得到XML字符串形式
			// 一个字符流，可以用其回收在字符串缓冲区中的输出来构造字符串
			// 创建SAX转换工厂
			SAXTransformerFactory sff = (SAXTransformerFactory)SAXTransformerFactory.newInstance();
			// 转换处理器，侦听 SAX ContentHandler
			//解析事件，并将它们转换为结果树 Result
			TransformerHandler th = sff.newTransformerHandler();
			// 将源树转换为结果树
			Transformer transformer = th.getTransformer();
			// 设置字符编码
			transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
			// 是否缩进
			transformer.setOutputProperty(OutputKeys.INDENT, "yes");

			//设置与用于转换的此 TransformerHandler 关联的 Result
			//注：这两个th.setResult不能同时启用
			th.setResult(resultXml);
			th.startDocument();
			AttributesImpl attr = new AttributesImpl();
			th.startElement("", "", "record", attr);

			for (Map<String,Object> map:  list )  {
				//设置属性
				//	attr.addAttribute("","","PERSONID","","E0"+i);
				th.startElement("", "", "row", attr);
				attr.clear();
                for(String key:map.keySet()) {
					th.startElement("", "", key, attr);
					String value = String.valueOf(map.get(key));
					th.characters(value.toCharArray(), 0, value.length());
					th.endElement("", "", key);
				}
			   th.endElement("", "", "row");
			}
			th.endElement("", "", "record");
			th.endDocument();

		} catch (TransformerConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void createZip(String zipfilename,  String[] filenameArray) {
		try {
			ZipUtils.zip(filenameArray, zipfilename);
		} catch (Exception e) {
			log.info("create ZIP  file err,cause:{}" + e.getMessage());
		}

	}



	public void  saxToXml() throws Exception{


			Result resultXml = new StreamResult(new FileOutputStream("E:\\workspace\\TestXml\\src\\com\\whm\\test.xml"));
			//用来得到XML字符串形式
			// 一个字符流，可以用其回收在字符串缓冲区中的输出来构造字符串
			// 构建转换结果树所需的信息。


			// 创建SAX转换工厂
			SAXTransformerFactory sff = (SAXTransformerFactory)SAXTransformerFactory.newInstance();
			// 转换处理器，侦听 SAX ContentHandler
			//解析事件，并将它们转换为结果树 Result
			TransformerHandler th = sff.newTransformerHandler();
			// 将源树转换为结果树
			Transformer transformer = th.getTransformer();
			// 设置字符编码
			transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
			// 是否缩进
			transformer.setOutputProperty(OutputKeys.INDENT, "yes");

			//设置与用于转换的此 TransformerHandler 关联的 Result
			//注：这两个th.setResult不能同时启用
			th.setResult(resultXml);
			th.startDocument();
			AttributesImpl attr = new AttributesImpl();
			th.startElement("", "", "PEOPLE", attr);

			for (int i = 0; i <5000000; i++) {
				attr.addAttribute("","","PERSONID","","E0"+i);
				th.startElement("", "", "PERSON", attr);
				attr.clear();

				th.startElement("", "", "NAME", attr);
				String id = String.valueOf("xuehui"+ i);
				th.characters(id.toCharArray(), 0, id.length());
				th.endElement("", "", "NAME");

				th.startElement("", "", "ADDRESS", attr);
				String address = String.valueOf("address"+i);
				th.characters(address.toCharArray(), 0, address.length());
				th.endElement("", "", "ADDRESS");

				th.startElement("", "", "TEL", attr);
				String tel = String.valueOf("tel"+i);
				th.characters(tel.toCharArray(), 0, tel.length());
				th.endElement("", "", "TEL");

				th.startElement("", "", "FAX", attr);
				String fax = String.valueOf("fax"+i);
				th.characters(fax.toCharArray(), 0, fax.length());
				th.endElement("", "", "FAX");

				th.startElement("", "", "EMAIL", attr);
				String email = String.valueOf("chenfu_20"+i+"@163.com");
				th.characters(email.toCharArray(), 0, email.length());
				th.endElement("", "", "EMAIL");

				th.endElement("", "", "PERSON");
				//attr.setAttribute(i, "", "", "PERSONID", "", ""+i);
			}
			//}
			th.endElement("", "", "PEOPLE");
			th.endDocument();



	}





}
